Session 3 - Basic Plots, Dynamic Output, and Manipulation

Creating plots

Plotting one dimensional functions

The function Plot[ f, {x, min, max} ] plots the expression f depending on the variable x between the values min and max. The argument f can also be a list of functions, all functions are plotted simultaneously.

(* Unset x *)
Unset[x]
(* Create the expression to plot. Note that f must not be a function, just an expression with a variable *)
f=2*Log[x];
(* Plot the expression *)
Plot[f,{x,0, 10}]
(* A list of expression generates multiple plots *)
Plot[{x+1,1/42*x^2,f},{x,0,10}]

Plot has several options to manipulate and style the output plot. The option Range -> {min, max} fixes the plot range, PlotLabel->”Title” sets a title of the plot... See documentation for a full list of options.

Plot[{Log[x],x+1,10-x},{x,0,10},PlotRange->{-10,10}, PlotLabel->"Test",PlotTheme->"Monochrome"]

Other one dimensional plots

The functions ListPlot or ListLinePlot can be used to plot points.

(* List Plots of one-dimensional data *)
ListPlot[{1,3,5,6,2,5}]
ListLinePlot[{1,3,5,6,2,5}]
(* List Plots of points *)
ListPlot[{{1,4},{3,0},{4,3},{7,5}}]
ListLinePlot[{{1,4},{3,0},{4,3},{7,5}}]

3D - Plots

Plot3D[Sin[x+y^2],{x,-3,3},{y,-2,2}]

Dynamic Output

Sometimes it is useful to have a dynamic output; that is a printed output that is constantly evaluated and updated with the current value while the computation is still running. This can be used to create a progress bar, for example.

(* Print a dynamic expression *)
Dynamic[progress]
(* Can also be used together with print *)
Print["Current progress is: ", Dynamic[progress]," %"];
(* Do some computations involving the progress variable, the output from above will now always be updated *)
For[progress=0,progress<100,progress++,
Pause[0.1];
];

Manipulating Output

The Manipulate[ ] function allows to create dynamic output that depends on a dynamic variable that can be set by the user. Use Manipluate as follows:
Manipulate [ expr, { var, min, max, step} ]
This creates a dynamic version of expr with the dynamic variable var that can take values between min and max with stepsize step (the stepsize is optional)
Note, that expression can be anything, e.g., numbers, symbolic expressions, solutions of equations and even plots and graphs

(* This create a dynamic output showing the n-th prime number with 1≤n≤100 *)
Manipulate[ Prime[n], {n,1,100,1}]

(* This creates an interactive Plot *)

Manipulate[Plot[Sin[a x+b],{x,0,6}],{a,1,4},{b,0,10}]

(* This shows the complete graph with n vertices where n can be chosen interavtively *)
Manipulate[CompleteGraph[n],{n,1,25,1}]